package com.bhatni.taskmanager;

import com.bhatni.taskmanager.task.Task;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class TaskList {

    private SortedMap scheduleTask = Collections.synchronizedSortedMap(new TreeMap());
    private LinkedList tasklist = new LinkedList();

    public void addTask(Task task) {
        this.tasklist.add(task);
    }

    public void addScheduleTask(Task task, Date schedule) {
        if (this.scheduleTask.containsKey(schedule)) {
            LinkedList list = (LinkedList) this.scheduleTask.get(schedule);
            list.add(task);
        } else {
            LinkedList list = new LinkedList();
            list.add(task);
            this.scheduleTask.put(schedule, list);
        }
    }

    public boolean hasScheduledTasks() {
        return !this.scheduleTask.isEmpty();
    }

    public Date getNextSchedule() {
        if (!this.scheduleTask.isEmpty()) {
            return (Date) this.scheduleTask.firstKey();
        }
        return null;
    }

    public void getPendingTasks(List pendingtasks) {
        long currenttime = System.currentTimeMillis();
        Date date = (Date) this.scheduleTask.firstKey();
        long firsttasktime = date.getTime();
        while ((firsttasktime - currenttime <= 0L) && (!this.scheduleTask.isEmpty())) {
            LinkedList list = (LinkedList) this.scheduleTask.remove(date);
            pendingtasks.addAll(list);
            if (!this.scheduleTask.isEmpty()) {
                date = (Date) this.scheduleTask.firstKey();
                firsttasktime = date.getTime();
            }
        }
    }
}
